package com.freak.freakmusic.model.base

import android.view.View
import androidx.fragment.app.Fragment
import com.freak.freakmusic.R
import com.freak.freakmusic.base.BaseAbstractSimpleActivity
import com.freak.freakmusic.model.base.adapter.MainTabAdapter
import com.freak.freakmusic.model.example.FIndFragment
import com.freak.freakmusic.model.example.ListenFragment
import com.freak.freakmusic.model.example.MineFragment
import com.freak.freakmusic.model.example.RecommendFragment
import com.freak.freakmusic.model.homepage.HomepageFragment
import com.freak.kotlinhttpmanager.kotlinhttpmanager.log.LogUtil
import kotlinx.android.synthetic.main.activity_main.*
import org.jetbrains.anko.toast

class MainActivity : BaseAbstractSimpleActivity(), View.OnClickListener {
    private lateinit var titleList: MutableList<String>
    private lateinit var fragmentList: MutableList<Fragment>
    private lateinit var homepageFragment: HomepageFragment
    private lateinit var listenFragment: ListenFragment
    private lateinit var recommendFragment: RecommendFragment
    private lateinit var findFragment: FIndFragment
    private lateinit var mineFragment: MineFragment
    override fun onClick(v: View?) {
        when (v!!.id) {
            R.id.constraint_layout_tab_homepage -> {
                toast("点击 0")
                setSelect(0)
            }
            R.id.constraint_layout_tab_listen -> {
                setSelect(1)
                LogUtil.e("点击 1")
                toast("点击 1")
            }
            R.id.constraint_layout_tab_recommend -> {
                setSelect(2)
                LogUtil.e("点击 2")
                toast("点击 2")
            }
            R.id.constraint_layout_tab_find -> {
                setSelect(3)
                LogUtil.e("点击 3")
                toast("点击 3")
            }
            R.id.constraint_layout_tab_mine -> {
                setSelect(4)
                LogUtil.e("点击 4")
                toast("点击 4")
            }
        }

    }

    override val layout: Int
        get() = R.layout.activity_main

    override fun initEventAndData() {
    }

    override fun onDestroyRelease() {
    }

    override fun initView() {
        titleList = arrayListOf()
        fragmentList = arrayListOf()
        homepageFragment = HomepageFragment()
        listenFragment = ListenFragment()
        recommendFragment = RecommendFragment()
        findFragment = FIndFragment()
        mineFragment = MineFragment()
        titleList.add("首页")
        titleList.add("我听")
        titleList.add("")
        titleList.add("发现")
        titleList.add("我的")
        fragmentList.add(homepageFragment)
        fragmentList.add(listenFragment)
        fragmentList.add(recommendFragment)
        fragmentList.add(findFragment)
        fragmentList.add(mineFragment)
        val mainTabAdapter = MainTabAdapter(
            supportFragmentManager,
            fragmentList as ArrayList<Fragment>, titleList as ArrayList<String>
        )

        view_pager_main.adapter = mainTabAdapter
        view_pager_main.offscreenPageLimit = 5
        constraint_layout_tab_homepage.setOnClickListener(this)
        constraint_layout_tab_listen.setOnClickListener(this)
        constraint_layout_tab_recommend.setOnClickListener(this)
        constraint_layout_tab_find.setOnClickListener(this)
        constraint_layout_tab_mine.setOnClickListener(this)
        setSelect(0)
    }

    private fun setSelect(i: Int) {
        view_pager_main.currentItem = i
        when (i) {
            0 -> {
                constraint_layout_tab_homepage.isSelected = true
                constraint_layout_tab_listen.isSelected = false
                constraint_layout_tab_recommend.isSelected = false
                constraint_layout_tab_find.isSelected = false
                constraint_layout_tab_mine.isSelected = false
            }
            1 -> {
                constraint_layout_tab_homepage.isSelected = false
                constraint_layout_tab_listen.isSelected = true
                constraint_layout_tab_recommend.isSelected = false
                constraint_layout_tab_find.isSelected = false
                constraint_layout_tab_mine.isSelected = false
            }
            2 -> {
                constraint_layout_tab_homepage.isSelected = false
                constraint_layout_tab_listen.isSelected = false
                constraint_layout_tab_recommend.isSelected = true
                constraint_layout_tab_find.isSelected = false
                constraint_layout_tab_mine.isSelected = false
            }
            3 -> {
                constraint_layout_tab_homepage.isSelected = false
                constraint_layout_tab_listen.isSelected = false
                constraint_layout_tab_recommend.isSelected = false
                constraint_layout_tab_find.isSelected = true
                constraint_layout_tab_mine.isSelected = false
            }
            4 -> {
                constraint_layout_tab_homepage.isSelected = false
                constraint_layout_tab_listen.isSelected = false
                constraint_layout_tab_recommend.isSelected = false
                constraint_layout_tab_find.isSelected = false
                constraint_layout_tab_mine.isSelected = true
            }
        }
    }

}
